package struts.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import struts.form.XmlBean;
import struts.util.StrutsConfigReader;
import annotation.ActionConfig;
import annotation.ActionConfigBean;
import annotation.util.PackagesToScanUtil;

public class ActionListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("信息：系统已经注销！");

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        ServletContext context = arg0.getServletContext();
        Set<Class<?>> classSet = PackagesToScanUtil.getClasses("business");
        Map<String, ActionConfigBean> pathActionConfMap = new HashMap<String, ActionConfigBean>();
        ActionConfigBean actionConfBean = null;
        for(Class<?> cls : classSet) {
            if(cls.isAnnotationPresent(ActionConfig.class)){
                actionConfBean = new ActionConfigBean();
                actionConfBean.setAction(cls.getAnnotation(ActionConfig.class).action());
                actionConfBean.setForm(cls.getAnnotation(ActionConfig.class).form());
                actionConfBean.setPath(cls.getAnnotation(ActionConfig.class).path());
                actionConfBean.setForwards(cls.getAnnotation(ActionConfig.class).forwards());
                pathActionConfMap.put(actionConfBean.getPath(), actionConfBean);
            }
        }
        
        context.setAttribute("struts", pathActionConfMap);
        System.out.println("信息：系统已经加载完成！");
        
        
    }

}
